/*
 * Copyright 2010 WithOne Co. Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package jp.co.withone.osgi.gadget.upnp;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class UpnpView extends JFrame implements ActionListener {

	// frame
	private static final int FRAME_WIDTH = 280;
	private static final int FRAME_HEIGHT = 330;
	// container
	private static final int CONTAINER_WIDTH = 280;
	private static final int CONTAINER_HEIGHT = 310;
	// mediaName
	private static final int MEDIA_NAME_WIDTH = 270;
	private static final int MEDIA_NAME_HEIGHT = 25;
	// topicPath
	private static final int TOPIC_PASS_WIDTH = 270;
	private static final int TOPIC_PASS_HEIGHT = 25;
	// upnpList
	private static final int UPNP_LIST_WIDTH = 270;
	private static final int UPNP_LIST_HEIGHT = 200;
	// button
	private static final int PANEL_BUTTON_WIDTH  = 270;
	private static final int PANEL_BUTTON_HEIGHT = 50;

	// タイトル
	private static final String FRAME_TITLE = "UPnP List";

	UpnpView() {
		initComponent();
	}
	
	private void initComponent() {
		
		UpnpViewComponent upnpView = new UpnpViewComponent();
		
		// タイトルバー設定
		setTitle(FRAME_TITLE);
	    // ウィンドウサイズ
	    setBounds(0, 0, FRAME_WIDTH, FRAME_HEIGHT);
	    // 終了処理
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
			SwingUtilities.updateComponentTreeUI(this);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// Container生成
		Container container = getContentPane();
		container.setLayout(null);
		container.setBounds(0, 0, CONTAINER_WIDTH, CONTAINER_HEIGHT);

		// mediaName
		upnpView.panelMediaName = new JPanel(new GridLayout(1,1));
		upnpView.panelMediaName.setBounds(0, 0, MEDIA_NAME_WIDTH, MEDIA_NAME_HEIGHT);
		upnpView.labelMediaName = new JLabel("MediaName", JLabel.LEFT);
		upnpView.labelMediaName.setPreferredSize(new Dimension(MEDIA_NAME_WIDTH, MEDIA_NAME_HEIGHT));
		upnpView.labelMediaName.setVerticalAlignment(JLabel.TOP);
		upnpView.panelMediaName.add(upnpView.labelMediaName);
		container.add(upnpView.panelMediaName);

		// topicPath
		upnpView.panelTopicPath = new JPanel(new GridLayout(1,1));
		upnpView.panelTopicPath.setBounds(0, MEDIA_NAME_HEIGHT, TOPIC_PASS_WIDTH, TOPIC_PASS_HEIGHT);
		upnpView.labelTopicPath = new JLabel("topicPath", JLabel.LEFT);
		upnpView.labelTopicPath.setPreferredSize(new Dimension(TOPIC_PASS_WIDTH, TOPIC_PASS_HEIGHT));
		upnpView.labelTopicPath.setVerticalAlignment(JLabel.TOP);
		upnpView.panelTopicPath.add(upnpView.labelTopicPath);
		container.add(upnpView.panelTopicPath);

		// upnpList
		upnpView.panelUpnpList = new JPanel(new GridLayout(1,1));
		upnpView.panelUpnpList.setBounds(0, MEDIA_NAME_HEIGHT + TOPIC_PASS_HEIGHT, UPNP_LIST_WIDTH, UPNP_LIST_HEIGHT);
		upnpView.labelUpnpList = new JLabel("upnpList", JLabel.LEFT);
		upnpView.labelUpnpList.setPreferredSize(new Dimension(UPNP_LIST_WIDTH, UPNP_LIST_HEIGHT));
		upnpView.labelUpnpList.setVerticalAlignment(JLabel.TOP);
		upnpView.panelUpnpList.add(upnpView.labelUpnpList);
		container.add(upnpView.panelUpnpList);

		// button
		upnpView.panelButton = new JPanel(new GridLayout(1,2));
		upnpView.panelButton.setBounds(0, MEDIA_NAME_HEIGHT + TOPIC_PASS_HEIGHT + UPNP_LIST_HEIGHT,
				PANEL_BUTTON_WIDTH, PANEL_BUTTON_HEIGHT);
		ImageIcon folderIcon = new ImageIcon("../src/image/folder.png");
		ImageIcon mediaIcon = new ImageIcon("../src/image/media.png");
		upnpView.button = new JButton("button", folderIcon);
		upnpView.button2 = new JButton("button2", mediaIcon);
		upnpView.panelButton.add(upnpView.button);
		upnpView.panelButton.add(upnpView.button2);

		// ボタンをアクションリスナに登録
		upnpView.button.addActionListener(this);
		upnpView.button2.addActionListener(this);
		container.add(upnpView.panelButton);
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		UpnpViewComponent upnpView = new UpnpViewComponent();

		System.out.println(e.getSource().toString());
		if (e.getSource() == upnpView.button) {
			upnpView.labelMediaName.setText("buttonが押されました。");
			upnpView.labelTopicPath.setText("★☆★");
		} else if(e.getSource() == upnpView.button2) {
			upnpView.labelMediaName.setText("★☆★");
			upnpView.labelTopicPath.setText("button2が押されました。");
		}
	}
}

class UpnpViewComponent {
	public JPanel panelMediaName;
	public JLabel labelMediaName;
	public JPanel panelTopicPath;
	public JLabel labelTopicPath;
	public JPanel panelUpnpList;
	public JLabel labelUpnpList;
	public JPanel panelButton;
	public JButton button;
	public JButton button2;
}

